package com.maning.mncalendar;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.maning.calendarlibrary.MNCalendarVertical;
import com.maning.calendarlibrary.adapter.MNCalendarVerticalItemAdapter;
import com.maning.calendarlibrary.listeners.OnCalendarRangeChooseListener;
import com.maning.calendarlibrary.model.MNCalendarItemModel;
import com.maning.calendarlibrary.model.MNCalendarVerticalConfig;
import com.maning.calendarlibrary.model.MonthModel;

import java.text.SimpleDateFormat;
import java.util.Date;

public class OtherActivity extends AppCompatActivity {

    private Context context;

    private MNCalendarVertical mnCalendarVertical;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        context = this;

        mnCalendarVertical = (MNCalendarVertical) findViewById(R.id.mnCalendarVertical);
        /**
         * 区间选取完成监听
         */
        mnCalendarVertical.setOnCalendarRangeChooseListener(new OnCalendarRangeChooseListener() {
            @Override
            public void onRangeDate(Date startDate, Date endDate) {
                String startTime = sdf.format(startDate);
                String endTime = sdf.format(endDate);
                Toast.makeText(context, "开始日期:" + startTime + ",结束日期:" + endTime, Toast.LENGTH_SHORT).show();
            }
        });

        MNCalendarVerticalItemAdapter.CalendarAdapter calendarAdapter=new MNCalendarVerticalItemAdapter.CalendarAdapter() {
            @Override
            public void bindView(MonthModel monthModel, MNCalendarItemModel mnCalendarItemModel, MNCalendarVerticalItemAdapter.CalendarViewHolder calendarViewHolder) {
//                Date currentDate = new Date();
                Date date = mnCalendarItemModel.getDate();
//                Lunar lunar = mnCalendarItemModel.getLunar();
                int month = monthModel.getMonth();
                if (date.getMonth() != month) {
                    calendarViewHolder.itemView.setVisibility(View.GONE);
                } else {
                    calendarViewHolder.itemView.setVisibility(View.VISIBLE);
                }
                //
                calendarViewHolder.tv_small.setVisibility(View.GONE);
                calendarViewHolder.iv_bg.setVisibility(View.GONE);
                calendarViewHolder.tv_small.setText("");

                calendarViewHolder.tvDay.setText(String.valueOf(date.getDate()));

                //不是本月的隐藏
//            Date currentDate = currentCalendar.getTime();
//            if (date.getMonth() != currentDate.getMonth()) {
//                calendarViewHolder.itemView.setVisibility(View.GONE);
//            }
                //阴历的显示
//                if (mnCalendarVerticalConfig.isMnCalendar_showLunar()) {
//                    calendarViewHolder.tv_small.setVisibility(View.VISIBLE);
//                    String lunarDayString = LunarCalendarUtils.getLunarDayString(lunar.lunarDay);
//                    calendarViewHolder.tv_small.setText(lunarDayString);
//                } else {
//                    calendarViewHolder.tv_small.setVisibility(View.GONE);
//                }

                //判断是不是当天
//                String position_yyy_MM_dd = MNConst.sdf_yyy_MM_dd.format(date);
//                if (now_yyy_mm_dd.equals(position_yyy_MM_dd)) {
//                    calendarViewHolder.tvDay.setText("今天");
//                }
//
//                //小于今天的颜色变灰
//                if (date.getTime() < nowDate.getTime()) {
//                    calendarViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorBeforeToday());
//                }
//
//                //判断是不是点击了起始日期
//                if (adapter.startDate != null && adapter.startDate == date) {
//                    calendarViewHolder.iv_bg.setVisibility(View.VISIBLE);
//                    calendarViewHolder.iv_bg.setBackgroundResource(com.maning.calendarlibrary.R.drawable.mn_selected_bg_start);
//                    calendarViewHolder.tv_small.setVisibility(View.VISIBLE);
//                    calendarViewHolder.tv_small.setText("开始");
//                    calendarViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
//                    calendarViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
//                    //动态修改颜色
//                    GradientDrawable myGrad = (GradientDrawable) calendarViewHolder.iv_bg.getBackground();
//                    myGrad.setColor(mnCalendarVerticalConfig.getMnCalendar_colorStartAndEndBg());
//                }
//                if (adapter.endDate != null && adapter.endDate == date) {
//                    calendarViewHolder.iv_bg.setVisibility(View.VISIBLE);
//                    calendarViewHolder.iv_bg.setBackgroundResource(com.maning.calendarlibrary.R.drawable.mn_selected_bg_end);
//                    calendarViewHolder.tv_small.setVisibility(View.VISIBLE);
//                    calendarViewHolder.tv_small.setText("结束");
//                    calendarViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
//                    calendarViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
//                    //动态修改颜色
//                    GradientDrawable myGrad = (GradientDrawable) calendarViewHolder.iv_bg.getBackground();
//                    myGrad.setColor(mnCalendarVerticalConfig.getMnCalendar_colorStartAndEndBg());
//                }
//
//                //判断是不是大于起始日期
//                if (adapter.startDate != null && adapter.endDate != null) {
//                    if (date.getTime() > adapter.startDate.getTime() && date.getTime() < adapter.endDate.getTime()) {
//                        calendarViewHolder.iv_bg.setVisibility(View.VISIBLE);
//                        calendarViewHolder.iv_bg.setBackgroundResource(com.maning.calendarlibrary.R.drawable.mn_selected_bg_centre);
//                        calendarViewHolder.tvDay.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
//                        calendarViewHolder.tv_small.setTextColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeText());
//                        //动态修改颜色
//                        GradientDrawable myGrad = (GradientDrawable) calendarViewHolder.iv_bg.getBackground();
//                        myGrad.setColor(mnCalendarVerticalConfig.getMnCalendar_colorRangeBg());
//                    }
//                }

//            calendarViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    MNCalendarItemModel mnCalendarItemModel = mDatas.get(position);
//
//                    Date dateClick = mnCalendarItemModel.getDate();
//
//                    //必须大于今天
////                    if (dateClick.getTime() < nowDate.getTime()) {
////                        Toast.makeText(context, "选择的日期必须大于今天", Toast.LENGTH_SHORT).show();
////                        return;
////                    }
//
//                    if (adapter.startDate != null && adapter.endDate != null) {
//                        adapter.startDate = null;
//                        adapter.endDate = null;
//                    }
//                    if (adapter.startDate == null) {
//                        adapter.startDate = dateClick;
//                    } else {
//                        //判断结束位置是不是大于开始位置
//                        long deteClickTime = dateClick.getTime();
//                        long deteStartTime = adapter.startDate.getTime();
//                        if (deteClickTime <= deteStartTime) {
//                            adapter.startDate = dateClick;
//                        } else {
//                            adapter.endDate = dateClick;
//                        }
//                    }
//                    //选取通知
//                    adapter.notifyChoose();
//                    //刷新
//                    notifyDataSetChanged();
//                    adapter.notifyDataSetChanged();
//                }
//            });
            }
        };
        mnCalendarVertical.setup(new MNCalendarVerticalConfig.Builder().build(),calendarAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_other, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_01:

                /**
                 *  自定义设置相关
                 */
                MNCalendarVerticalConfig mnCalendarVerticalConfig = new MNCalendarVerticalConfig.Builder()
                        .setMnCalendar_showWeek(true)                   //是否显示星期栏
                        .setMnCalendar_showLunar(true)                  //是否显示阴历
                        .setMnCalendar_colorWeek("#B07219")             //星期栏的颜色
                        .setMnCalendar_titleFormat("yyyy-MM")           //每个月的标题样式
                        .setMnCalendar_colorTitle("#FF0000")            //每个月标题的颜色
                        .setMnCalendar_colorSolar("#ff0fc7")            //阳历的颜色
                        .setMnCalendar_colorLunar("#00ff00")            //阴历的颜色
                        .setMnCalendar_colorBeforeToday("#F1EDBD")      //今天之前的日期的颜色
                        .setMnCalendar_colorRangeBg("#9930C553")        //区间中间的背景颜色
                        .setMnCalendar_colorRangeText("#000000")        //区间文字的颜色
                        .setMnCalendar_colorStartAndEndBg("#258C3E")    //开始结束的背景颜色
                        .setMnCalendar_countMonth(120)                    //显示多少月(默认6个月)
                        .build();
                mnCalendarVertical.setConfig(mnCalendarVerticalConfig);
                break;
            case R.id.action_02:
                //隐藏星期
                MNCalendarVerticalConfig mnCalendarVerticalConfig2 = new MNCalendarVerticalConfig.Builder()
                        .setMnCalendar_showWeek(false)
                        .build();
                mnCalendarVertical.setConfig(mnCalendarVerticalConfig2);
                break;
            case R.id.action_03:
                //隐藏阴历
                MNCalendarVerticalConfig mnCalendarVerticalConfig3 = new MNCalendarVerticalConfig.Builder()
                        .setMnCalendar_showLunar(false)
                        .build();
                mnCalendarVertical.setConfig(mnCalendarVerticalConfig3);
                break;
            case R.id.action_04:
                //恢复默认
                MNCalendarVerticalConfig mnCalendarVerticalConfig4 = new MNCalendarVerticalConfig.Builder().build();
                mnCalendarVertical.setConfig(mnCalendarVerticalConfig4);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
